package com.shuigu.ai.model.vo;

import dev.langchain4j.data.message.*;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class MessageVO {

    private String messageType;
    private String thinking;
    private String text;

    public static List<MessageVO> serializationMessage(List<ChatMessage> chatMessages) {

        List<MessageVO> messageVOs = new ArrayList<>();
        for (ChatMessage chatMessage : chatMessages){
            if(chatMessage.type() == ChatMessageType.AI){
                AiMessage message = (AiMessage) chatMessage;
                if(message.hasToolExecutionRequests()){
                    List<String> toolExecutionRequests = message.toolExecutionRequests().stream().map(toolExecutionRequest -> {
                        return "请求调用工具：" + toolExecutionRequest.name();
                    }).collect(Collectors.toList());
                    String toolExecutionRequestsString = toolExecutionRequests.stream().collect(Collectors.joining("\n"));
                    if(message.text() != null && !toolExecutionRequestsString.isEmpty()){
                        toolExecutionRequestsString = toolExecutionRequestsString + "\n" + message.text();
                    }
                    messageVOs.add(new MessageVO(chatMessage.type().name(),message.thinking(), toolExecutionRequestsString));
                }else{
                    messageVOs.add(new MessageVO(chatMessage.type().name(),message.thinking(), message.text()));
                }
            }else if(chatMessage.type() == ChatMessageType.USER){
                UserMessage message = (UserMessage) chatMessage;
                messageVOs.add(new MessageVO(chatMessage.type().name(),"", message.singleText()));
            } else if(chatMessage.type() == ChatMessageType.TOOL_EXECUTION_RESULT){
                ToolExecutionResultMessage message = (ToolExecutionResultMessage) chatMessage;
                messageVOs.add(new MessageVO(chatMessage.type().name(),"", message.text()));
            }
//            else if(chatMessage.type() == ChatMessageType.SYSTEM){
//                SystemMessage message = (SystemMessage) chatMessage;
//                messageVOs.add(new MessageVO(chatMessage.type().name(),"", message.text()));
//            }

        }
        return messageVOs;
    }
}
